//
//  XWPressView.m
//  XWCollectionViewLayout
//
//  Created by 温仲斌 on 15/12/29.
//  Copyright © 2015年 温仲斌. All rights reserved.
//

#import "XWPressView.h"

@interface XWPressView (){
    double add;
    CATextLayer *textL;
}

@property (nonatomic, strong) CAGradientLayer *gradientLayer;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;


@end

@implementation XWPressView

@synthesize shapeLayer;

- (instancetype)init
{
    self                   = [super init];
    if (self) {
        self.frame             = [UIScreen mainScreen].bounds;
        self.gradientLayer     = [CAGradientLayer layer];
        _gradientLayer.frame   = self.frame;
        [_gradientLayer setColors:[NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor],(id)[[UIColor cyanColor] CGColor] ,[[UIColor orangeColor] CGColor],[[UIColor greenColor] CGColor], nil]];
        [_gradientLayer setStartPoint:CGPointMake(0, 0)];
        [_gradientLayer setEndPoint:CGPointMake(1, 1)];
        _gradientLayer.type    = kCAGradientLayerAxial;
        UIBezierPath *be       = [UIBezierPath bezierPath];
        [be addArcWithCenter:CGPointMake(50, 50) radius:50 startAngle:0 endAngle:2 * M_PI clockwise:NO];
        shapeLayer             = [CAShapeLayer layer];
        shapeLayer.path        = be.CGPath;
        shapeLayer.fillColor   = [UIColor clearColor].CGColor;
        shapeLayer.strokeColor = [UIColor orangeColor].CGColor;
        shapeLayer.strokeEnd   = 0.96f;
        shapeLayer.strokeStart = 0.f;
        shapeLayer.lineWidth   = 2;
        shapeLayer.bounds = CGRectMake(0, 0, 100, 100);
        shapeLayer.position         = CGPointMake(50, 15);
        
        textL                  = [CATextLayer layer];
        textL.string           = @" 加载中...";
        textL.contentsScale    = [UIScreen mainScreen].scale;
        textL.position         = CGPointMake(self.center.x, self.center.y);

        textL.bounds           = CGRectMake(0, 0, 100, 100);
        textL.fontSize         = 20;
        textL.alignmentMode    = kCAAlignmentCenter;
        [textL addSublayer:shapeLayer];
        
        
        [_gradientLayer setMask:textL];
        [self.layer addSublayer:_gradientLayer];
        
        CABasicAnimation *anim;
        anim=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        anim.duration = 1;
        anim.autoreverses=NO;
        anim.repeatCount= 10000;
        anim.fromValue=[NSNumber numberWithFloat:0];
        anim.toValue=[NSNumber numberWithFloat:2 * M_PI];
        //  layer1.transform=CATransform3DTranslate(CATransform3DIdentity, 0, 0, 100);
        [shapeLayer addAnimation:anim forKey:@"layer1Rotation"];
        
        
        add                    = 0.1;//每次递增0.1
//        用定时器模拟数值输入的情况
        self.timer             = [NSTimer scheduledTimerWithTimeInterval:1
                                                                  target:self
                                                                selector:@selector(circleAnimationTypeOne)
                                                                userInfo:nil
                                                                 repeats:YES];
//
//
        
    }
    return self;
}

- (void)circleAnimationTypeOne {
    
    //定时改变分割点
    self.gradientLayer.locations = @[@(arc4random() % 5 / 10.0f),@(arc4random() % 5 / 10.0f + 0.5f), @(1.0f)];
    
    
    
}

@end
